\documentclass[11pt]{article}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage[parfill]{parskip}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}

\title{CMSI 488 Assignment Two}
\author{Matt Kronk, Andrew Forney, and David Hara (Team MAD)}
\date{\today}
\begin{document}
\maketitle
\begin{center}
\emph{Find the source code at:} \\ http://code.google.com/p/david-hara-homework/source/browse/ \\
ROFLKODE Team effort located entirely in David's repository
\begin{verbatim}












\end{verbatim}
\end{center}
\textbf{Bonus:} \\
\newline David +1 (3/3/11: JavaCC syntax error caught) \\
\newline Andrew +2 (Both on 2/24/11: Two "a" function definitions; caught "up" instead of "+" in ROFLKODE)

\newpage
\section{Polynomial Grammar}
\begin{verbatim}
Microsyntax:

  <INT> -> (\x20)* (0-9)+ (\x20)*
  <VAR> -> (\x20)* "x" (\x20)* (\^ INT)?

Macrosyntax:

  S          -> POLYNOMIAL <EOF>
  POLYNOMIAL -> ("-")? TERM ([+-] TERM)*
  TERM       -> (<INT> (<VAR>)?)
  	          |  (<VAR>)

\end{verbatim}

\newpage
\section{Polynomial.jj}
\begin{verbatim}
PARSER_BEGIN(Eval)

// Horrid spacing throughout for LaTeX
package edu.lmu.cs.mad;

abstract class Polynomial {
    public abstract double evaluate();
}

class Term extends Polynomial {
    int coefficient;
    int power;
    double value;
    Term(int c, int p, double v) {
        coefficient = c;
        power = p;
        value = v;
    }
    Term(int c, Term t) {
        coefficient = c;
        power = t.power;
        value = t.value;
    }
    public double evaluate () {
        return Math.pow(value, power) * coefficient;
    }
    
    public void negate () {
        coefficient = -coefficient;
    }
}

class BinaryExp extends Polynomial {
    String op;
    Polynomial left, right;
    BinaryExp(String o, Polynomial l, Polynomial r) {
        op = o;
        left = l;
        right = r;
    }
    public double evaluate () {
        if (op.equals("+")) {
            return left.evaluate() + right.evaluate();
        } else if (op.equals("-")) {
            return left.evaluate() - right.evaluate();
        }
        return 0;
    }
}

public class Eval {
    public static void main(String[] args) {
        try {
            double result = new Eval(new java.io.StringReader(args[0]))
            .S(Double.parseDouble(args[1]));
            System.out.println(result);
        } catch (Throwable e) {
            // Catching Throwable is ugly but JavaCC throws Error objects!
            System.out.println("Syntax check failed: " + e.getMessage());
        }   
    }
}

PARSER_END(Eval)

SKIP:  { " " | "\t" }
TOKEN: { "+" | "-" | <NUM: (["0"-"9"])+> | "x" | "^" }

double S(double value): {Polynomial p;} 
{ 
    p=POLYNOMIAL(value) <EOF> {return p.evaluate();}
}
Polynomial POLYNOMIAL(double value): 
    {Polynomial p1; Polynomial p2; String negate = ""; String op;}
{
    ("-" {negate="-";})? p1=TERM(value) {if(negate.equals("-"))
     ((Term)p1).negate(); } (("+"{op="+";}|"-"{op="-";}) 
     p2=TERM(value) {p1 = new BinaryExp(op, p1, p2);})* 
     {return p1;}
}
Polynomial TERM(double value): {Token n; Polynomial t;}
{
    n=<NUM> {t = new Term(Integer.parseInt(n.image), 0, value);} 
    (t=VAR(value) {t = new Term(Integer.parseInt(n.image), (Term)t);} )? 
    {return t;}
|
    t=VAR(value) {return t;}
}
Polynomial VAR(double value): {Token power; Polynomial t;}
{
    "x" {t = new Term(1, 1, value);} ("^"power=<NUM> 
    {t = new Term(1, Integer.parseInt(power.image), value);})? 
    {return t;}
}
\end{verbatim}

\newpage
\section{PolynomialTest.java}
\begin{verbatim}
import org.junit.Assert;
import org.junit.Test;

import edu.lmu.cs.mad.Eval;
import edu.lmu.cs.mad.ParseException;

public class PolynomialTest {
        final double DELTA = 0.00001;
        
        @Test
        public void testEval () throws ParseException {
                
                // constant fun!
                Assert.assertEquals(0, new Eval(
                    new java.io.StringReader("0")).S(2.0), DELTA);
                Assert.assertEquals(5.0, new Eval(
                	 new java.io.StringReader("5")).S(-2.0), DELTA);
                Assert.assertEquals(-5.0, new Eval(
                	 new java.io.StringReader("-5")).S(-202.0), DELTA);
                Assert.assertEquals(8.0, new Eval(
                	 new java.io.StringReader("5 + 3")).S(42.0), DELTA);
                Assert.assertEquals(8.0, new Eval(
                	 new java.io.StringReader("5    +    3")).S(2.0), DELTA);
                Assert.assertEquals(8.0, new Eval(
                	 new java.io.StringReader("5+3")).S(79.0), DELTA);
                Assert.assertEquals(-7.0, new Eval(
                	 new java.io.StringReader("-5-2")).S(1.0), DELTA);
                Assert.assertEquals(-5.0, new Eval(
                	 new java.io.StringReader("-5+0")).S(2.0), DELTA);
                Assert.assertEquals(0.0, new Eval(
                	 new java.io.StringReader("-5+5-5+5")).S(4.0), DELTA);
                
                // degree one in the sun!
                Assert.assertEquals(2.0, new Eval(
                	 new java.io.StringReader("x")).S(2.0), DELTA);
                Assert.assertEquals(0.0, new Eval(
                	 new java.io.StringReader("0x")).S(287.0), DELTA);
                Assert.assertEquals(2.0, new Eval(
                	 new java.io.StringReader("x^1")).S(2.0), DELTA);
                Assert.assertEquals(-16.0, new Eval(
                	 new java.io.StringReader("5x + 4")).S(-4.0), DELTA);
                Assert.assertEquals(3.0, new Eval(
                	 new java.io.StringReader("-5x+4+3x+7-2x")).S(2.0), DELTA);
                
                // multi degree gotta bee!
                Assert.assertEquals(8.0, new Eval(
                	 new java.io.StringReader("-5x^2 + 4 x ^ 3 -2x^1")).S(2.0), DELTA);
                Assert.assertEquals(-3.0, new Eval(
                	 new java.io.StringReader("0x^ 4 + 5x - 2x^5")).S(-1.0), DELTA);
                Assert.assertEquals(79.0, new Eval(
                	 new java.io.StringReader("-5x^0 + 5x^4 - 2x")).S(-2.0), DELTA);
        }
}
\end{verbatim}

\end{document}